import { getToken } from '@/utils/auth'

// 登录页面
// const loginPage = "/pages/login"
const loginPage = "/pages/mine/index"
  
// 页面白名单 '/',
const whiteList = [
  '/','/pages/index',
  '/pages/work/index','/pages/mine/index','/pages/classify/index','/pages/shopping/index','/pagesA/userXieYi/userXieYi',
  '/pagesA/userYinSi/userYinSi','/pages/goodsfind/index','/pages/goodsfind/goodsList','/pages/goodslist/index',
  '/pages/goodsdetail/index','/pagesA/order/index','/pages/mine/login/index','/pages/mine/register/index'
]
  

// 检查地址白名单
function checkWhite(url) {
  const path = url.split('?')[0]
  return whiteList.indexOf(path) !== -1
}

// 页面跳转验证拦截器
let list = ["navigateTo", "redirectTo", "reLaunch", "switchTab"]
list.forEach(item => {
  uni.addInterceptor(item, {
    invoke(to) {
		console.log(to.url,'_______进入拦截____--')
		
		//如果token为true，表明登录成功。
		if (getToken()) {
			console.log('______Token存在____--',to.url)
		
		 //loginPage = "/pages/login"
		 //如果登录成功，依然跳转登录页面则跳转 “/”
		 //  if (to.url === loginPage) {
			// console.log('_______已经登录成功，依然跳转登录《login》页面则跳转 “/”______')
		 //    uni.reLaunch({ url: "/" })
		 //  }
		  return true
		} else {
		  if (checkWhite(to.url)) {
			  console.log('_______白名单存在____未登录状态--')
			  return true
		  }
		  uni.showToast({
		  	title: '未登录.',
			icon:'none'
		  });
		  return false
		}
		
      // if (getToken()) {
      //   if (to.url === loginPage) {
      //     uni.reLaunch({ url: "/" })
      //   }
      //   return true
      // } else {
      //   if (checkWhite(to.url)) {
      //     return true
      //   }
      //   uni.reLaunch({ url: loginPage })
      //   return false
      // }
    },
    fail(err) {
      console.log(err)
    }
  })
})
